package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.CategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;
    //添加分类
    @PostMapping
    public R save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功");
    }

    //分页查询
    @GetMapping("page")
  public R<Page<Category>> page(Integer page,Integer pageSize){
        //1.创建分页对象
        Page<Category> pageInfo = new Page<>(page,pageSize);

        //2.创造一个条件构造对象
        categoryService.lambdaQuery()
            .orderByAsc(Category::getSort)  //根据排序升序
            .orderByDesc(Category::getUpdateTime)//根据修改时间降序
            .page(pageInfo);    //分页查询
        return R.success(pageInfo);
    }

    //修改分类
    @PutMapping
    public R<String> update(@RequestBody Category category, HttpSession session){
        //update employee set status =? ,update_time = ? update_user = ? where id = ?
        categoryService.updateById(category);//employee对象中属性为空的会自动忽略
        return R.success("修改成功");
    }

    //删除分类
    @DeleteMapping
    public R<String> deleteById(Long id){
        categoryService.deleteById(id);
        return R.success("删除成功");
    }

    /**
     * 根据type查询菜品分类列表
     */
    @GetMapping("list")
    public R<List<Category>> list(Integer type){
       //查询分类数据 select * from category where type = ？

        List<Category> list = categoryService
            .lambdaQuery()
            .eq(type != null, Category::getType, type)
            .orderByDesc(Category::getSort)
            .list();

        return R.success(list);
    }


}
